package com.wty.basic.array.Arrays;

import java.util.Arrays;
import java.util.List;

/**
 * 对Arrays工具类的一些实践
 * @Author wangtengyu
 * @Create 2018-08-06-15:00
 */
public class Test {
    //测试Arrays.Fill()
    @org.junit.Test
    public  void testFill(){
        int a[]=new int[5];
        Arrays.fill(a,3);//填充所有元素为3
        Arrays.fill(a,1,3,4);//填充索引1-2的元素为3 包左不包右

    }

    //测试排序
    @org.junit.Test
    public  void testSort(){
        int a[]=new int[]{2,1,32,13,14,7,24};
        Arrays.sort(a);
        for (int x:a){
            System.out.print(x+" ");
        }
    }

    //测试比较
    @org.junit.Test
    public  void testEquals(){
        int a[]=new int[]{1,2,3,4,5};
        int b[]=new int[]{5,4,3,2,1};
        int c[]=new int[]{1,2,3,4,5};
        System.out.println(Arrays.equals(a,b));
        System.out.println(Arrays.equals(a,c));
    }

    @org.junit.Test
    public  void testEquals2(){
        boolean a[]=new boolean[]{true,true};
        boolean b[]=new boolean[]{false,true};
        System.out.println(Arrays.equals(a,b));
    }



    //测试deepEquals
    @org.junit.Test
    public  void testDeepEquals(){
        int a[][]=new int[][]{{1,2},{2,3},{4,5}};
        int b[][]=new int[][]{{1,2},{2,3},{4,5}};
        System.out.println(Arrays.equals(a,b));
        System.out.println(Arrays.deepEquals(a,b));
    }

    //测试二分查找 要求数组有序
    @org.junit.Test
    public void testBinarySearch(){
        int a[]=new int[]{5,4,3,2,1};//倒序也是排序
        int b[]=new int[]{1,2,3,4,5};
        int c[]=new int[]{5,2,3,4,1};
        //Arrays.sort(c);
        int indexA=Arrays.binarySearch(a,3);
        int indexB=Arrays.binarySearch(b,5);
        int indexC=Arrays.binarySearch(c,1);
        System.out.println(a[indexA]);
        System.out.println(b[indexB]);
        System.out.println(indexC);
        System.out.println(c[indexC]);
    }

    //测试截取
    @org.junit.Test
    public void testCopy(){
        int a[]=new int[]{1,2,3,4,5,6,7,8,9,10};
        int copyOf[]=Arrays.copyOf(a,5);
        for (int x:copyOf){
            System.out.print(x+"  ");//1,2,3,4,5
        }
        System.out.println();
        System.out.println("长度不够0来补位");
        int copyOfOutLength[]=Arrays.copyOf(a,12);
        for (int x:copyOfOutLength){
            System.out.print(x+"  ");//长度不够0来补位
        }
        System.out.println();
        System.out.println("----------------------");
        int copyOfRange[]=Arrays.copyOfRange(a,3,7);
        for (int x:copyOfRange){
            System.out.print(x+"  ");//4,5,6,7
        }
    }

    //测试toString
    @org.junit.Test
    public void testToString(){
        Person a[]=new Person[]{new Person("张三"),new Person("李四")};
        System.out.println(Arrays.toString(a));

        int b[]=new int[]{1,2,3,4,5};
        System.out.println(Arrays.toString(b));


        int c[][]=new int[][]{{1,2},{2,3},{4,5}};
        System.out.println(Arrays.toString(c));
        System.out.println(Arrays.deepToString(c));
        System.out.println();
    }

    @org.junit.Test
    public void testToList(){
        int a[]=new int[]{1,2,3,4,5};
        List intList =Arrays.asList(a);
        for (Object o:intList){
            System.out.println(o);
        }


        String s[]=new String[]{"aa","bb","cc"};
        List<String> strList=Arrays.asList(s);
        for (String str:strList){
            System.out.println(str);
        }
    }



}
